package com.hzpd.hainan.module.usercenter.acticity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hzpd.hainan.R;
import com.hzpd.hainan.app.ToolBarActivity;
import com.hzpd.hainan.config.CODE;
import com.hzpd.hainan.model.SuperEntity;
import com.hzpd.hainan.model.usercenter.EmptyEntity;
import com.hzpd.hainan.net.Factory;
import com.hzpd.hainan.utils.LogUtils;
import com.hzpd.hainan.utils.ParamUtils;
import com.hzpd.hainan.utils.TUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class ChangePwActivity extends ToolBarActivity {
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.oldpw_activity_pw)
    EditText oldpw_activity_pw;
    @BindView(R.id.new1_pw_activity_change)
    EditText new1_pw_activity_change;
    @BindView(R.id.new2_pw_activity_change)
    EditText new2_pw_activity_change;
    @BindView(R.id.ok_pw_activity_change)
    Button ok_pw_activity_change;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (spu.getLanguage().equals(CODE.ZHONGWEN)) {
            tv_title.setText("修改密码");
            oldpw_activity_pw.setHint("旧密码");
            new1_pw_activity_change.setHint("新密码");
            new2_pw_activity_change.setHint("确认新密码");
            ok_pw_activity_change.setText("确定");
        }else{
            tv_title.setText("གསང་གྲངས་བརྗེ་བ།");
            oldpw_activity_pw.setHint("སྔར་གྱི་གསང་གྲངས།");
            new1_pw_activity_change.setHint("གསང་གྲངས་གསར་བ།");
            new2_pw_activity_change.setHint("གསང་གྲངས་གཏན་འཁེལ།");
            ok_pw_activity_change.setText("གཏན་འཁེལ།");
        }
    }

    @Override
    public String initToolBarTitle() {
        return null;
    }

    @Override
    public int setMyContentView() {
        return R.layout.activity_change_pw;
    }

    @OnClick(R.id.ok_pw_activity_change)
    public void changePW(View view) {

        String old = oldpw_activity_pw.getText().toString().trim();
        String new1 = new1_pw_activity_change.getText().toString().trim().trim();
        String new2 = new2_pw_activity_change.getText().toString();
//        Intent  intent=getIntent();
//        type=intent.getIntExtra(InterfaceJsonfile.PWDTYPE,2);

        if (old == null || "".equals(old)) {
            TUtils.toast("请输入旧密码");
            return;
        }
//        if (!old.equals(spu.getUser().getPassword())){
//            TUtils.toast("密码输入不正确");
//            return;
//        }
        if (new1 == null || "".equals(new1)) {
            TUtils.toast("请输入新密码");
            return;
        }
        if (new2 == null || "".equals(new2)) {
            TUtils.toast("请再次输入新密码");
            return;
        }
        if (!new1.equals(new2)) {
            TUtils.toast("两次输入的结果不一致");
            new1_pw_activity_change.setText("");
            oldpw_activity_pw.setText("");
            new2_pw_activity_change.setText("");
            return;
        }

        if (new1.length() < 6) {
            TUtils.toast("密码不能太短");
            return;
        }
        if (new1.length() > 12) {
            TUtils.toast("密码不能太长");
            return;
        }
        showDialog();

        changePwd(old, new2);

    }

    private void changePwd(String oldpwd, String newPwd) {

        Map<String, String> map = new HashMap<>();
        map.put("uid", spu.getUser().getUid());

        map.put("oldpassword", oldpwd);
        map.put("newpassword", newPwd);
        Factory.provideHttpService().changePassWord(ParamUtils.getRequestParam(map))
                .onBackpressureBuffer()
                .subscribeOn(Schedulers.io())
                .filter(new Func1<EmptyEntity, Boolean>() {
                    @Override
                    public Boolean call(EmptyEntity emptyEntity) {

                        LogUtils.e("PW---" + emptyEntity.code);
//                        if ("200".equals(superEntity.code)){
//
//                            return true;
//                        }
                        return true;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<EmptyEntity>() {
                    @Override
                    public void call(EmptyEntity emptyEntity) {
                        disMissDialog();
                        if ("200".equals(emptyEntity.code)) {
                            TUtils.toast(emptyEntity.msg);
                            activity.finish();
                        } else {
                            TUtils.toast(emptyEntity.msg);
                        }


                    }

                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });

    }
}
